//index.js
var api = require("../../utils/api.js");
Page({
  data: {
    outvisitManagement: [
      { icon: "06", text: "预约查勘", url: "../outvisit/task/appointmentList", show: "Y" },
      { icon: "03", text: "我的查勘", url: "../outvisit/task/taskList?type=2", show: "Y" },
      { icon: "05", text: "已完成查勘", url: "../outvisit/task/taskList?type=3", show: "Y" },
      { icon: "02", text: "地图", url: "../map/map?type=0", show: "Y" },
    ]
  },

  onLoad: function () {
    var that = this;
    //验证是否登录 
    var token = wx.getStorageSync('token')
    if (token != undefined && token != "") {
      // 获取权限信息
      //that.getUserRole();
    }
  },

  // 获取权限信息
  getUserRole: function () {
    var that = this;
    wx.showLoading({
      title: "加载中...",
    });
    api.getUserRole().then(res => {
      if (res.code==0){
        wx.hideLoading();
        var data=res.data;
        // 设置有权限功能显示
        that.setFunShow(data);
      }
    });
  },

  // 设置有权限功能显示
  setFunShow: function (data) {
    var that = this;
    var outvisitManagement = that.data.outvisitManagement;
    var OAManagement = that.data.OAManagement;
    var manage = that.data.manage;

    // 外访审批权限
    if (data.waitAudited == 1) {
      outvisitManagement[6].show = "Y";
    }
    // OA审批权限
    if (data.oaAudited == 1) {
      OAManagement[1].show = "Y";
    }
    // 统计权限
    if (data.overall == 1) {
      manage[0].show = "Y";
    }
    that.setData({
      outvisitManagement: outvisitManagement,
      OAManagement: OAManagement,
      manage: manage
    });
  },

  //事件处理函数
  bindViewTap: function (option) {
    var that = this;
    var url = option.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    })
  },

  // 搜索跳转
  searchTo: function (option) {
    wx.navigateTo({
      url: '../search/search?type=1'
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //验证是否登录 
    var token = wx.getStorageSync('token')
    if (token == undefined || token == "") {
      //需要重新登录
      wx.redirectTo({
        url: '../login/login'
      });
    }
  }
})
